﻿using System.Text;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Json2Class;

class Program
{
    static void Main(string[] args)
    {
        Utils.WriteLine("🎇-----HOW TO USE-----", ConsoleColor.Magenta);
        Utils.WriteLine("1. Enter or paste JSON string; ", ConsoleColor.Green);
        Utils.WriteLine("2. Enter \"/\" in a new line to end; ", ConsoleColor.Green);
        Utils.WriteLine("-----------------------------------------", ConsoleColor.Green);
        string json = string.Empty;
        string lineStr = string.Empty;
        do
        {
            json += lineStr;
            lineStr = Console.ReadLine()??"";
        }
        while (lineStr != "/");

        var generator = new CSharpJson2Class();
        if(!generator.TryGenerate(json, out string csharpCode))
        {
            Utils.WriteLine("----生成失败----", ConsoleColor.Red);
            Utils.WriteLine(csharpCode, ConsoleColor.Red);
            return;
        }
        //Utils.WriteLine("");
        var lines = csharpCode.Split('\n');
        Utils.WriteLine("﹀﹀﹀﹀﹀GENERATED C# CLASSES﹀﹀﹀﹀﹀", ConsoleColor.Magenta);
        foreach (var line in lines)
        {
            WriteLineCode(line);
        }
        Utils.WriteLine("︿︿︿︿︿︿︿︿︿E-N-D︿︿︿︿︿︿︿︿︿", ConsoleColor.Magenta);
    }

    public static void WriteLineCode(string line)
    {
        var words = line.Split(' ');
        bool isPropName = false;
        for (int i = 0; i < words.Length; i++)
        {
            var word = words[i];
            if (word == "public")
            {
               Utils.Write(word, ConsoleColor.Blue); 
               isPropName = true;
            }
            else if (word == "class")
            {
               Utils.Write(word, ConsoleColor.Blue); 
            }
            else if(isPropName)
            {
                Utils.Write(word, ConsoleColor.Green);
                isPropName = false;
            }
            else if (word == "get;" || word == "set;")
            {
                Utils.Write(word, ConsoleColor.Blue);
            }
            else if(Regex.IsMatch(word, @"^\[JsonProperty"))
            {
                Utils.Write(word, ConsoleColor.DarkGray);
            }
            else
            {
                Utils.Write(word);
            }
            if (i < words.Length - 1)
                Utils.Write(" ");                
        }
        Utils.WriteLine("");
    }
    
}